/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.tor.tribes.reportserver.util;

import de.tor.tribes.types.ZebraReport;
import de.tor.tribes.types.ZebraReportBuilding;
import de.tor.tribes.types.ZebraReportUnit;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang.StringEscapeUtils;

/**
 *
 * @author Torridity
 */
public class ReportFormatter {

    private final static String WINNER_STRING = "\\$WINNER_STRING";
    private final static String SEND_TIME = "\\$SEND_TIME";
    private final static String LUCK_STRING = "\\$LUCK_STRING";
    private final static String LUCK_BAR = "\\$LUCK_BAR";
    private final static String MORAL = "\\$MORAL";
    private final static String ATTACKER = "\\$ATTACKER";
    private final static String SOURCE = "\\$SOURCE";
    private final static String DEFENDER = "\\$DEFENDER";
    private final static String TARGET = "\\$TARGET";
    private final static String ATTACKER_TABLE = "\\$ATTACKER_TABLE";
    private final static String DEFENDER_TABLE = "\\$DEFENDER_TABLE";
    private final static String MISC_TABLES = "\\$MISC_TABLES";
    private final static String RAM_DAMAGE = "\\$RAM_DAMAGE";
    private final static String CATA_DAMAGE = "\\$CATA_DAMAGE";
    private final static String SNOB_INFLUENCE = "\\$SNOB_INFLUENCE";
    private final static String LUCK_NEG = "\\$LUCK_NEG";
    private final static String LUCK_POS = "\\$LUCK_POS";
    private final static String LUCK_ICON1 = "\\$LUCK_ICON1";
    private final static String LUCK_ICON2 = "\\$LUCK_ICON2";
    private static String pTemplateData = "";

    static {
        FileReader fr = null;
        try {
            fr = new FileReader(new File("H:/Software/DSWorkbench/templates/report.tmpl"));
            BufferedReader r = new BufferedReader(fr);
            String line = "";
            while ((line = r.readLine()) != null) {
                pTemplateData += line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException ioe) {
                }
            }
        }
    }

    public static String buildToolTip(ZebraReport pReport) {
        String res = pTemplateData;
        SimpleDateFormat f = new SimpleDateFormat("dd.MM.yy HH:mm:ss");
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMinimumFractionDigits(0);
        nf.setMaximumFractionDigits(0);
        res = res.replaceAll(WINNER_STRING, ((pReport.isWon() ? "Der Angreifer hat gewonnen" : "Der Verteidiger hat gewonnen")));
        res = res.replaceAll(SEND_TIME, f.format(new Date(pReport.getSent())));
        res = res.replaceAll(ATTACKER_TABLE, "<img src=\"" + UnitTableBuilder.createUnitTableLink(pReport, ZebraReportUnit.TYPE.ATTACKER) + "\"/>");
        res = res.replaceAll(DEFENDER_TABLE, "<img src=\"" + UnitTableBuilder.createUnitTableLink(pReport, ZebraReportUnit.TYPE.DEFENDER) + "\"/>");



        res = res.replaceAll(MISC_TABLES, "");



        res = res.replaceAll(LUCK_STRING, "Gl&uuml;ck (aus Sicht des Angreifers)");
        res = res.replaceAll(LUCK_BAR, buildLuckBar(pReport.getLuck()));
        res = res.replaceAll(MORAL, nf.format(pReport.getMoral()) + "%");
        nf.setMinimumFractionDigits(1);
        nf.setMaximumFractionDigits(1);
        res = res.replaceAll(LUCK_NEG, ((pReport.getLuck() < 0) ? "<b>" + nf.format(pReport.getLuck()) + "%</b>" : ""));
        res = res.replaceAll(LUCK_POS, ((pReport.getLuck() >= 0) ? "<b>" + nf.format(pReport.getLuck()) + "%</b>" : ""));
        //  res = res.replaceAll(LUCK_ICON1, "<img src=\"" + ((pReport.getLuck() <= 0) ? FightReportHTMLToolTipGenerator.class.getResource("/res/rabe.png") : FightReportHTMLToolTipGenerator.class.getResource("/res/rabe_grau.png")) + "\"/>");
        //  res = res.replaceAll(LUCK_ICON2, "<img src=\"" + ((pReport.getLuck() >= 0) ? FightReportHTMLToolTipGenerator.class.getResource("/res/klee.png") : FightReportHTMLToolTipGenerator.class.getResource("/res/klee_grau.png")) + "\"/>");
        res = res.replaceAll(ATTACKER, StringEscapeUtils.escapeHtml(pReport.getAttacker()));
        res = res.replaceAll(SOURCE, StringEscapeUtils.escapeHtml(pReport.getSource()));
        res = res.replaceAll(DEFENDER, StringEscapeUtils.escapeHtml(pReport.getDefender()));
        res = res.replaceAll(TARGET, StringEscapeUtils.escapeHtml(pReport.getTarget()));

        ZebraReportBuilding buildingDamage = pReport.getBuildingDamage();
        ZebraReportBuilding wallDamage = pReport.getWallDamage();
        res = res.replaceAll(RAM_DAMAGE, ((wallDamage != null) ? "Wall besch&auml;digt von Level <b>" + wallDamage.getLevelBefore() + "</b> auf Level <b>" + wallDamage.getLevelAfter() + "</b>" : ""));
        res = res.replaceAll(CATA_DAMAGE, ((buildingDamage != null) ? buildingDamage.getBuilding().getName() + " besch&auml;digt von Level <b>" + buildingDamage.getLevelBefore() + "</b> auf Level <b>" + buildingDamage.getLevelAfter() + "</b>" : ""));
        res = res.replaceAll(SNOB_INFLUENCE, ((pReport.wasSnobAttack()) ? "Zustimmung gesunken von <b>" + pReport.getAcceptBefore() + "</b> auf <b>" + pReport.getAcceptAfter() + "</b>" : ""));

        return res;
    }

    /*    private static String buildMiscTables(FightReport pReport) {
     String res = "";
     if (!pReport.isConquered()) {
     res += "<tr>";
     res += "<td colspan=\"5\">&nbsp;</td>";
     res += "</tr>";
     return res;
     }

     String onTheWayTable = "<table width=\"100%\" style=\"border: solid 1px black; padding: 4px;background-color:#EFEBDF;\">";
     String outsideTable = "<table width=\"100%\" style=\"border: solid 1px black; padding: 4px;background-color:#EFEBDF;\">";
     onTheWayTable += "<tr>";
     outsideTable += "<tr>";


     String headerRow = "<tr>";
     String onTheWayRow = "<tr>";

     for (UnitHolder unit : DataHolder.getSingleton().getUnits()) {
     headerRow += "<td style=\"background-color:#E1D5BE;\"><img src=\"" + FightReportHTMLToolTipGenerator.class.getResource("/res/ui/" + unit.getPlainName() + ".png") + "\"/></td>";

     int amount = 0;
     if (pReport.whereDefendersOnTheWay()) {
     amount = pReport.getDefendersOnTheWay().get(unit);
     }
     if (amount == 0) {
     onTheWayRow += "<td style=\"color:#DED3B9;\">" + amount + "</td>";
     } else {
     onTheWayRow += "<td>" + amount + "</td>";
     }
     }
     headerRow += "</tr>";
     onTheWayRow += "</tr>";

     onTheWayTable += headerRow;
     onTheWayTable += onTheWayRow;

     onTheWayTable += "</table>";
     res = "<tr>";
     res += "<td colspan=\"5\"><b>Truppen des Verteidigers, die unterwegs waren:</td>";
     res += "</tr>";
     res += "<tr>";
     res += "<td colspan=\"5\">";
     res += onTheWayTable;
     res += "</td>";
     res += "</tr>";
     String outsideRow = "";

     if (pReport.whereDefendersOutside()) {
     headerRow = "<tr style=\"background-color:#E1D5BE;\"><td width=\"100\">&nbsp;</td>";
     for (UnitHolder unit : DataHolder.getSingleton().getUnits()) {
     headerRow += "<td><img src=\"" + FightReportHTMLToolTipGenerator.class.getResource("/res/ui/" + unit.getPlainName() + ".png") + "\"/></td>";
     }
     headerRow += "</tr>";

     Enumeration<Village> outside = pReport.getDefendersOutside().keys();
     while (outside.hasMoreElements()) {
     Village v = outside.nextElement();
     outsideRow += "<tr><td width=\"100\"><div align=\"center\">" + v.getFullName() + "</div></td>";

     for (UnitHolder unit : DataHolder.getSingleton().getUnits()) {
     int amount = pReport.getDefendersOutside().get(v).get(unit);
     if (amount == 0) {
     outsideRow += "<td style=\"color:#DED3B9;\">" + amount + "</td>";
     } else {
     outsideRow += "<td>" + amount + "</td>";
     }
     }
     outsideRow += "</tr>";
     }

     res += "<tr>";
     res += "<td colspan=\"5\"><b>Truppen ausserhalb:</td>";
     res += "</tr>";
     res += "<tr>";
     res += "<td colspan=\"5\">";
     outsideTable += headerRow;
     outsideTable += outsideRow;
     outsideTable += "</table>";
     res += outsideTable;
     res += "</td>";
     res += "</tr>";
     }
     return res;
     }
     */
    public static String buildLuckBar(double pLuck) {
        String res = "<table cellspacing=\"0\" cellpadding=\"0\" style=\"border: solid 1px black; padding: 0px;\">";
        res += "<tr>";
        if (pLuck == 0) {
            res += "<td width=\"" + 50 + "\" height=\"12\"></td>";
            res += "<td width=\"" + 0 + "\" style=\"background-color:#FF0000;\"></td>";
            res += "<td width=\"2\" style=\"background-color:rgb(0, 0, 0)\"></td>";
            res += "<td width=\"0\" style=\"background-color:#009300\"></td>";
            res += "<td width=\"50\"></td>";
        } else if (pLuck < 0) {
            double luck = Math.abs(pLuck);
            double filled = luck / 25 * 50;
            double notFilled = 50 - filled;
            res += "<td width=\"" + notFilled + "\" height=\"12\"></td>";
            res += "<td width=\"" + filled + "\" style=\"background-color:#FF0000;\"></td>";
            res += "<td width=\"2\" style=\"background-color:rgb(0, 0, 0)\"></td>";
            res += "<td width=\"0\" style=\"background-color:#009300\"></td>";
            res += "<td width=\"50\"></td>";
        } else {
            double filled = pLuck / 25 * 50;
            double notFilled = 50 - filled;
            res += "<td width=\"50\" height=\"12\"></td>";
            res += "<td width=\"0\" style=\"background-color:#F00;\"></td>";
            res += "<td width=\"2\" style=\"background-color:rgb(0, 0, 0)\"></td>";
            res += "<td width=\"" + filled + "\" style=\"background-color:#009300\"></td>";
            res += "<td width=\"" + notFilled + "\"></td>";
        }

        res += "</tr>";
        res += "</table>";
        return res;
    }

    public static String buildLuckBar(String pResourcePath, double pLuck) {
        StringBuilder b = new StringBuilder();
        b.append("<table cellspacing=\"0\" cellpadding=\"0\" style=\"border: solid 1px #DDDDDD; padding: 0px;\">");
        b.append("<tr>");
        if (pLuck < 0) {
            b.append("<td><img src=\"").append(pResourcePath).append("VAADIN/themes/zebra/img/rabe.png\"/></td>");
        } else {
            b.append("<td><img src=\"").append(pResourcePath).append("VAADIN/themes/zebra/img/rabe_grau.png\"/></td>");
        }
        if (pLuck == 0) {
            b.append("<td width=\"").append(50).append("\" height=\"12\"></td>");
            b.append("<td width=\"").append(0).append("\" style=\"background-color:#FF0000;\"></td>");
            b.append("<td width=\"2\" style=\"background-color:rgb(0, 0, 0)\"></td>");
            b.append("<td width=\"0\" style=\"background-color:#009300\"></td>");
            b.append("<td width=\"50\"></td>");
        } else if (pLuck < 0) {
            double luck = Math.abs(pLuck);
            double filled = luck / 25 * 50;
            double notFilled = 50 - filled;
            b.append("<td width=\"").append(notFilled).append("\" height=\"12\"></td>");
            b.append("<td width=\"").append(filled).append("\" style=\"background-color:#FF0000;\"></td>");
            b.append("<td width=\"2\" style=\"background-color:rgb(0, 0, 0)\"></td>");
            b.append("<td width=\"0\" style=\"background-color:#009300\"></td>");
            b.append("<td width=\"50\"></td>");
        } else {
            double filled = pLuck / 25 * 50;
            double notFilled = 50 - filled;
            b.append("<td width=\"50\" height=\"12\"></td>");
            b.append("<td width=\"0\" style=\"background-color:#F00;\"></td>");
            b.append("<td width=\"2\" style=\"background-color:rgb(0, 0, 0)\"></td>");
            b.append("<td width=\"").append(filled).append("\" style=\"background-color:#009300\"></td>");
            b.append("<td width=\"").append(notFilled).append("\"></td>");
        }

        if (pLuck < 0) {
            b.append("<td><img src=\"").append(pResourcePath).append("VAADIN/themes/zebra/img/klee_grau.png\"/></td>");
        } else {
            b.append("<td><img src=\"").append(pResourcePath).append("VAADIN/themes/zebra/img/klee.png\"/></td>");
        }

        b.append("</tr>");
        b.append("</table>");
        return b.toString();
    }
}
